package com.njit.WeStudy.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReplaceFileContent {
    public static String replaceFileContent(String filePath,String content){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            Path templatePath= Paths.get(filePath);
            BufferedReader temp= Files.newBufferedReader(templatePath);//防止读一行隔一行
            BufferedReader bufferedReader= Files.newBufferedReader(templatePath);
            while (temp.readLine() != null){
                stringBuilder.append(bufferedReader.readLine());
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将字符串中的$符号进行正规化处理
        String formalContent = java.util.regex.Matcher.quoteReplacement(content);
        String templateContent;
        templateContent = stringBuilder.toString().replaceAll("####",formalContent);
        return templateContent;
    }
}
